/**
 * Created by dukes on 2015/9/23.
 */
class FrameEffect extends egret.Bitmap{
   public data:EffectConfig;
    public playTimes:number=0;
    public currentPlayTimes:number=0;
    public currentFrame:number=1;
    public completeCallObj:any;
    public completeCallFun:Function;
    public completeCallParam:any;
    public rx:number;
    public ry:number;
    private currentGap:number = 0;
    public autoDispose:boolean;
    private isLoaded:boolean;
    public constructor(){
        super();
        this.name = "frameEffect"+this.hashCode;
        this.touchEnabled = false;
        this.cacheAsBitmap = true;
    }
    public update(delta:number){
        this.currentGap+=delta;
        if( this.currentGap >= this.data.playgap){
            this.currentGap = 0;
            this.nextFrame();
        }
    }
    public checkLoad(){
        var res = RES.getRes(this.data.resPath);
        if( res){
            this.isLoaded = true;
        }else{
            RES.getResAsync(this.data.resPath,this.checkLoad,this);
        }
    }
    public nextFrame(){
        if( !this.isLoaded ) return;
        this.texture = RES.getRes(this.data.resPrefix+this.currentFrame);
        this.x = this.rx - (this.texture.textureWidth >> 1);
        this.y = this.ry - (this.texture.textureHeight >> 1);
        this.currentFrame++;
        if( this.currentFrame > this.data.totalFrame) {
            this.currentFrame = 1;
            if( this.playTimes != -1){
                this.currentPlayTimes += 1;
                if( this.currentPlayTimes >= this.playTimes){
                    if( this.completeCallFun != null && this.completeCallObj != null)
                        this.completeCallFun.call(this.completeCallObj,this.completeCallParam);

                    EffectMgr.getInstance().stopEffect(this);

                }
            }
        }
    }

    public reset(){
        this.playTimes = 0;
        this.currentPlayTimes = 0;
        this.currentFrame = 1;
        this.completeCallObj = null;
        this.completeCallFun = null;
        this.autoDispose = false;
        this.isLoaded = false;
    }
}